package usbcali.edu.co.pqrs.dataaccess.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import usbcali.edu.co.pqrs.dataaccess.api.HibernateDaoImpl;
import usbcali.edu.co.pqrs.modelo.SolicitudPqr;
import usbcali.edu.co.pqrs.modelo.dto.AnxsXAreaDTO;
import usbcali.edu.co.pqrs.modelo.dto.AreaDTO;
import usbcali.edu.co.pqrs.modelo.dto.DetalleDTO;
import usbcali.edu.co.pqrs.modelo.dto.SolicitudAsignadaAreaDTO;
import usbcali.edu.co.pqrs.modelo.dto.SolicitudPqrDTO;

/**
 * A data access object (DAO) providing persistence and search support for
 * SolicitudPqr entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see lidis.SolicitudPqr
 */
@Scope("singleton")
@Repository("SolicitudPqrDAO")
public class SolicitudPqrDAO extends HibernateDaoImpl<SolicitudPqr, Long>
		implements ISolicitudPqrDAO {
	private static final Logger log = LoggerFactory
			.getLogger(SolicitudPqrDAO.class);
	@Resource
	private SessionFactory sessionFactory;

	public static ISolicitudPqrDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ISolicitudPqrDAO) ctx.getBean("SolicitudPqrDAO");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SolicitudPqrDTO> traerSolicitud(Long idTipo,
			String nombreCliente) {
		List<SolicitudPqrDTO> list = null;

		try {

			Query query = getSession().getNamedQuery("traer_solicitud");
			query.setParameter("pIdTpSolPqr_TipoSolicitudPqr", idTipo);
			query.setParameter("pNombreCliente", nombreCliente);
			query.setResultTransformer(Transformers
					.aliasToBean(SolicitudPqrDTO.class));
			list = query.list();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AreaDTO> consultarNombreArea(String numeroRadicacion,
			Long tipoEstado, Long tipoSolPqr, String fechaDesde,
			String fechaHasta) throws Exception {
		List<AreaDTO> listDto = null;
		try {

			Query query = getSession().getNamedQuery("consulta_nombre_area");

			// query.setParameter("pNumeroRadicacion", numeroRadicacion);
			query.setParameter("pTipoSolPqr", tipoSolPqr);
			query.setParameter("pIdTipoEstaPqr", tipoEstado);
			query.setParameter("pFechaDesde", fechaDesde);
			query.setParameter("pFechaHasta", fechaHasta);
			query.setResultTransformer(Transformers.aliasToBean(AreaDTO.class));
			listDto = query.list();

		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		return listDto;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DetalleDTO> consultarDetalle(String numeroRadicacion)
			throws Exception {
		List<DetalleDTO> listDto = null;
		try {

			Query query = getSession().getNamedQuery("consulta_detalle");

			query.setParameter("pNumeroRadicacion", numeroRadicacion);

			query.setResultTransformer(Transformers
					.aliasToBean(DetalleDTO.class));
			listDto = query.list();

		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		return listDto;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SolicitudAsignadaAreaDTO> consultarAreaAsignada(Long idSolicitud)
			throws Exception {
		List<SolicitudAsignadaAreaDTO> listDto = null;
		try {

			Query query = getSession().getNamedQuery("traer_area_asignada");

			query.setParameter("pIdSolPqr_SolicitudPqr", idSolicitud);
			query.setResultTransformer(Transformers.aliasToBean(SolicitudAsignadaAreaDTO.class));
			listDto = query.list();

		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		return listDto;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SolicitudPqrDTO> getDataSolicitudPqrByLogin(String login)
			throws Exception {
		List<SolicitudPqrDTO> listDto = null;
		try {

			Query query = getSession().getNamedQuery("traer_solicitudes_del_area");

			query.setParameter("pLogin", login);
			query.setResultTransformer(Transformers.aliasToBean(SolicitudPqrDTO.class));
			listDto = query.list();

		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		return listDto;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AnxsXAreaDTO> traerAnexosPorArea(Long idArea) throws Exception {
		List<AnxsXAreaDTO> listDto = null;
		try {

			Query query = getSession().getNamedQuery("anexos_por_area");

			query.setParameter("pIdArea", idArea);
			query.setResultTransformer(Transformers.aliasToBean(AnxsXAreaDTO.class));
			listDto = query.list();

		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		return listDto;
	}

}
